﻿using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using GameTools.Models;
using Loli.Library.Util;
using Loli.Library.WinForm;
using Loli.Library.WinIo;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GameTools.Classes
{
    public class PlayerAction
    {
        //吃药
        public static bool EatDrug(DrugType drugType)
        {
            DataCenter dc = DataCenter.Load();
            int potion = 0;
            KeyCodes keyCode = KeyCodes.None;
            if (drugType == DrugType.Life)
            {
                keyCode = KeyCodes.PageUp;
                potion = dc.Config.LifePotion;
            }
            else
            {
                keyCode = KeyCodes.PageDown;
                potion = dc.Config.MagicPotion;
            }

            for (int i = 0; i < potion; i++)
            {
                lock (dc._KeyLock)
                {
                    dc._Action.KeyPress(keyCode, 80);
                }
                Thread.Sleep(100);
            }


            return true;
        }

        //验证bar
        public static bool CheckLifeOrMagic(BarTypes barType, Player player)
        {
            DataCenter dc = DataCenter.Load();
            LifeAndMagic bar = null;
            bool isSuccess = true;
            float rate = 0;

            if (barType == BarTypes.Life)
            {
                bar = player.Life;
                rate = dc.Config.LifeRate / 100f;
            }
            else
            {
                bar = player.Magic;
                rate = dc.Config.MagicRate / 100f;
            }

            //截图
            if (bar.Image != null)
            {
                bar.Image.Dispose();
            }

            bar.Image = LoliCode.Capture(bar);

            int x = Convert.ToInt32(Math.Round(bar.Image.Width * rate));
            //取2点
            Color pixel1 = bar.Image.GetPixel(x, 2);
            Color pixel2 = bar.Image.GetPixel(x, bar.Height - 2);


            if (pixel1.R == pixel1.G && pixel1.G == pixel1.B && pixel2.R == pixel2.G && pixel2.G == pixel2.B)
            {
                isSuccess = false;
            }

            return isSuccess;
        }

        public static Point FindPlayerLocation(Map map, Image<Bgr, byte> template)
        {
            //获取小地图
            if (map.Image != null)
            {
                map.Image.Dispose();
            }

            map.Image = LoliCode.Capture(map);

            List<Point> list = null;
            using (Image<Bgr, byte> sourceImage = new Image<Bgr, byte>(map.Image))
            {
                list = EmguCV.TemplateMatching(sourceImage, template, 0.85);
            }
            return list.FirstOrDefault();
        }

        public static bool FindMonster(DataCenter dc, Point location, Directions direction, Step step, INoticeForm notice)
        {
            Map map = dc.CurrentMap;
            Player player = dc.CurrentPlayer;
            IntPtr handle = dc.Handle;

            if (dc.MonsterImage != null)
            {
                dc.MonsterImage.Dispose();
            }

            //获取阶梯大小
            int w = direction == Directions.Left ? (location.X - step.Left.X) * 15 : (step.Right.X - location.X) * 15;
            if (w < dc.Config.MoveDistance)
            {
                w = dc.Config.MoveDistance;
            }

            if (w < map.MonsterMark.Width)
            {
                w = map.MonsterMark.Width + 1;
            }

            Size monsterSize = MapleStory.GetMonsterSize();

            if (w < monsterSize.Width)
            {
                monsterSize.Width = w;
            }

            //获取全屏截图
            using (Image<Gray, byte> full = LoliCode.Capture(handle))
            {
                List<Point> points = EmguCV.TemplateMatching(full, player.Mark1, 0.85);

                if (points.Count <= 0 && player.Mark2 != null)
                {
                    points = EmguCV.TemplateMatching(full, player.Mark2, 0.85);
                }

                if (points.Count <= 0)
                {
                    notice.ShowMessage("未找到人物！");
                    return false;
                }

                Point point = points.FirstOrDefault();

                //取图像
                Bitmap bmp = direction == Directions.Left
                      ? BitmapUtil.Cut(
                        full.Bitmap, point.X - monsterSize.Width,
                        point.Y - monsterSize.Height, monsterSize.Width, monsterSize.Height)
                      : BitmapUtil.Cut(
                        full.Bitmap, point.X - monsterSize.Width,
                        point.Y - monsterSize.Height, monsterSize.Width, monsterSize.Height);


                dc.MonsterImage = new Image<Gray, byte>(bmp);

                bmp.Dispose();

                List<Point> list = EmguCV.TemplateMatching(dc.MonsterImage, map.MonsterMark, 0.9);


                //匹配怪物
                return list.Count > 0;
            }
        }

        public static void Attack(DataCenter dc, IEnumerable<Skill> skills)
        {
            foreach (var skill in skills)
            {                
                lock (dc._SkillLock)
                {
                    lock (dc._KeyLock)
                    {
                        dc._Action.KeyPress(skill.KeyCode, 100);
                    }
                    Thread.Sleep(skill.ActionTime);
                }
            }
        }

    }
}

